<?php

namespace App\Admin\Controllers;

use App\Models\Link;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class LinksController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('友情链接管理');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑友情链接');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建友情链接');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Link::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('网站名称');
            $grid->url('网站链接');
            $grid->sort('排序');
            $grid->state('审核')->display(function ($value) {
                return $value ? '已审核' : '未审核';
            });
            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Link::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name', '网站名称')->rules('required');
            $form->url('url', '网站链接')->rules('required');
            $form->text('logo', 'LOGO路径');
            $form->radio('type', '文字或LOGO')->options(['1' => 'LOGO', '0'=> '文字'])->default('0');
            $form->text('info', '描述文字')->rules('required');
            $form->number('sort', '排序')->min(1)->default('1')->rules('required|numeric|min:1');
            $form->radio('state', '审核')->options(['1' => '已审核', '0'=> '未审核'])->default('0');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更改时间');
        });
    }
}
